import { WeightType } from '@/models/targetWeight'
import { PageParam } from '@/services/common'
import { covertTableData } from '@/utils/tableUtil'
import { request } from '@umijs/max'

export interface TargetWeightVO {
  /**
   * id
   */
  id: number
  /**
   * 类型
   */
  type: string
  /**
   * 关联id
   */
  relationId: number
  /**
   * 权重
   */
  width: number
  /**
   * 百分比
   */
  percentage: number
  /**
   * 租户id
   */
  tenantId: number
}

export interface TargetWeightQueryDTO extends PageParam {
  type?: string
  month: string
}

export interface TargetWeightUpdateDTO {
  /**
   * id
   */
  id: number
  /**
   * 类型
   */
  type: string
  /**
   * 关联id
   */
  relationId: number
  /**
   * 权重
   */
  width: number
  /**
   * 百分比
   */
  percentage: number
}

/**
 * 指标类型
 */
export type IndexType = 'sales' | 'customer'

export interface WeightVO {
  id?: string
  name: string
  month: string
  type: WeightType
  relationId: string
  width: number
  percentage: number
  currentPercentage: number
  target: number
  currentTargetValue: number
  indexType: IndexType
  unit: string
}

export const getTargetWeightById = async (id: string) => {
  return request<TargetWeightVO>(`/api/targetWeight?id=${id}`, { method: 'GET' })
}

export const getTargetWeightList = async (params: TargetWeightQueryDTO) => {
  return request<Array<TargetWeightVO>>(`/api/targetWeight/list`, { method: 'GET', params })
}

export const getTargetWeightPage = async (params: TargetWeightQueryDTO) => {
  let response = await request<Array<TargetWeightVO>>(`/api/targetWeight/page`, { method: 'GET', params })
  return covertTableData(response)
}

export const updateTargetWeight = async (targetWeightUpdateDTO: TargetWeightUpdateDTO) => {
  return request<TargetWeightVO>(`/api/targetWeight/update`, { method: 'POST', data: targetWeightUpdateDTO })
}

export const saveTargetWeight = async (targetWeightUpdateDTO: TargetWeightUpdateDTO) => {
  return request<TargetWeightVO>(`/api/targetWeight/save`, { method: 'POST', data: targetWeightUpdateDTO })
}

export const removeTargetWeight = async (id: string) => {
  return request<boolean>(`/api/targetWeight/remove?id=${id}`, { method: 'POST' })
}

/**
 * 更新权重数据
 * @param data
 */
export const updateWeight = (data: WeightVO[]) => {
  return request<WeightVO[]>(`/api/targetWeight/updateWeightAndTarget`, { method: 'POST', data })
}

/**
 * 获取目标聚合结果
 */
export const getTargetData = (params: TargetWeightQueryDTO) => {
  return request<WeightVO[]>(`/api/targetWeight/getTargetData`, { method: 'GET', params })
}
